1894E - Freedom of Choice - CodeForces Solution


brute force data structures greedy implementation

Please click on ads to support us..

C++ Code:

//#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define int long long

typedef long long ll;
typedef unsigned long long ull;
typedef long double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;

const int N = 2e5 + 10, inf = 0x3f3f3f3f3f3f3f3f, mod = 998244353;

int m;
int l[N], r[N];
vii a(N), c(N);
int sum[N];
set<int> st;
map<int, int> mp;
map<int, int> mpmn;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
    {
        cin >> m;
        for (int i = 1; i <= m; i++)
            a[i].clear(), c[i].clear(), sum[i] = 0;
        st.clear();
        mp.clear();
        mpmn.clear();
        int suml = 0, sumr = 0;
        for (int i = 1; i <= m; i++)
        {
            int n;
            cin >> n >> l[i] >> r[i];
            suml += l[i], sumr += r[i];
            for (int j = 0; j < n; j++)
            {
                int x;
                cin >> x;
                a[i].push_back(x);
            }
            for (int j = 0; j < n; j++)
            {
                int x;
                cin >> x;
                c[i].push_back(x);
                sum[i] += x;
            }
        }
        for (int i = 1; i <= m; i++)
        {
            for (int j = 0; j < a[i].size(); j++)
                if (a[i][j] >= suml && a[i][j] <= sumr)
                {
                    st.insert(a[i][j]);
                    mp[a[i][j]] += max(c[i][j] - (sum[i] - r[i]), 0ll);
                    mpmn[a[i][j]] += max(c[i][j] - (sum[i] - l[i]), 0ll);
                }
        }
        if (st.size() < sumr - suml + 1)
        {
            cout << 0 << endl;
            continue;
        }
        int ans = INT64_MAX;
        for (auto it : st)
            ans = min(ans, max(mp[it] - (sumr - it), mpmn[it]));
        cout << ans << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude